package com.lc.guessTheWords.layer;

import java.util.HashMap;

import android.graphics.Typeface;

import com.lc.guessTheWords.R;
import com.lc.guessTheWords.core.GameSystem;
import com.lc.guessTheWords.core.StageManager;
import com.lc.guessTheWords.entity.StageData;
import com.lc.guessTheWords.scene.MainScene;
import com.lc.guessTheWords.sprite.StageBlock;
import com.lc.guessTheWords.util.GameUtil;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.NinePatchSprite;
import com.wiyun.engine.nodes.ScrollableLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

public class StageLayer extends Layer {

	Sprite background;
	Sprite title_feng;
	Sprite title_kuang;
	Sprite title_tian;
	Sprite title_zi;

	Button btnReturn;
	Label stageTip;
	ScrollableLayer layer;

	HashMap<Integer, StageBlock> stages = new HashMap<Integer, StageBlock>();

	public StageLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_game_background);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		background.setScaleX(GameSystem.Screen_Width
				/ GameSystem.tex_game_background.getWidth());
		background.setScaleY(GameSystem.Screen_Height
				/ GameSystem.tex_game_background.getHeight());
		this.addChild(background);
		loadTitle();
		loadStages();

		// return button
		btnReturn = Button.make(R.drawable.button_return,
				R.drawable.button_return_pressed, this, "onReturnClick");
		btnReturn.setPosition(411, 745);
		addChild(btnReturn);

		stageTip = Label.make("完成50%以上的词条可以解锁后下一个关卡", 22, Typeface.BOLD);
		stageTip.setColor(WYColor3B.make(255, 255, 255));
		stageTip.setAnchorY(0);
		stageTip.setPosition(240, 8);
		addChild(stageTip);

		scollToIndex(StageManager.instance.getLastStage());
	}

	private void loadStages() {
		layer = ScrollableLayer.make();
		layer.setContentSize(GameSystem.Screen_Width,
				GameSystem.Screen_Height - 150);
		layer.setRelativeAnchorPoint(true);
		layer.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 - 25);
		layer.setVertical(true);
		layer.setLeftMargin((GameSystem.Screen_Width - GameSystem.tex_stage_block
				.getWidth()) / 2);
		layer.setRightMargin((GameSystem.Screen_Width - GameSystem.tex_stage_block
				.getWidth()) / 2);
		layer.setTopMargin(50);
		layer.setBottomMargin(55);
		addChild(layer);

		// create thumb bar
		NinePatchSprite vThumb = NinePatchSprite.make(
				Texture2D.makePNG(R.drawable.vertical_thumb),
				WYRect.make(DP(5), DP(7), DP(1), DP(1)));

		// set thumb bar
		layer.setVerticalThumb(vThumb);
		layer.setThumbFadeOutTime(2.f);

		for (int i = 0; i < GameSystem.stageCount; i++) {
			int stageIndex = GameSystem.stageCount - i;
			StageBlock stageBlock = new StageBlock(stageIndex);
			stageBlock.setPosition(0, 90 * i);
			layer.addScrollableChild(stageBlock);
			stages.put(stageIndex, stageBlock);

			if (StageManager.instance.getStageDatas().containsKey(stageIndex)) {
				stageBlock.setLockStatus(false);
			} else {
				stageBlock.setLockStatus(true);
			}
		}
	}

	public void unlockStage() {
		// 先手动写吧
		// for (int i = 4; i <= GameSystem.stageCount; i = i + 3) {
		// if (!StageManager.instance.getStageDatas().containsKey(i)) {
		// // 需要判断解锁
		// if (getComplateRate(i - 1) >= 0.5f
		// && getComplateRate(i - 2) >= 0.5f
		// && getComplateRate(i - 3) >= 0.5f) {
		//
		// StageManager.instance.getStageDatas().put(i,
		// new StageData());
		// StageManager.instance.getStageDatas().put(i + 1,
		// new StageData());
		// StageManager.instance.getStageDatas().put(i + 2,
		// new StageData());
		// StageManager.instance.saveCurrentStageData(i);
		// StageManager.instance.saveCurrentStageData(i + 1);
		// StageManager.instance.saveCurrentStageData(i + 2);
		// }
		// break;
		// }
		// }

		// 新，一个一个判断解锁
		for (int i = 2; i <= GameSystem.stageCount; i++) {
			if (!StageManager.instance.getStageDatas().containsKey(i)) {
				// 需要判断解锁
				if (getComplateRate(i - 1) >= 0.5f) {
					StageManager.instance.getStageDatas().put(i,
							new StageData());
					StageManager.instance.saveCurrentStageData(i);
				}
				break;
			}
		}
	}

	/**
	 * 当用户购买了解锁所有关卡后如果增加了新的关卡，需要调用一下
	 */
	public void unlockExtraStageOnBuyAllStage() {
		// 已经购买了解锁所有关卡，以及关卡数量发生变化的情况下
		if (StageManager.instance.isBuyAllStages()
				&& StageManager.instance.getLastStageCount() < GameSystem.stageCount) {
			StageManager.instance.unlockAllStages();
			StageManager.instance.setLastStageCount(GameSystem.stageCount);
		}
	}

	private float getComplateRate(int i) {
		if (StageManager.instance.getStageDatas().get(i) == null) {
			return 0f;
		}
		return StageManager.instance.getStageDatas().get(i).getComplateCount()
				/ (float) StageManager.instance.mapWordCounts.get(i);
	}

	public void refreshStages() {
		for (int i = 1; i <= GameSystem.stageCount; i++) {
			if (StageManager.instance.getStageDatas().containsKey(i)) {
				stages.get(i).setLockStatus(false);
				// 更新关卡完成情况
				stages.get(i).refreshStageInfo();
			} else {
				stages.get(i).setLockStatus(true);
			}

		}
	}

	public void scollToIndex(int index) {
		if (StageManager.instance.getLastStage() > 0) {
			layer.setOffsetY((index - 1) * 90);
		}
	}

	private float DP(float v) {
		return ResolutionIndependent.resolveDp(v);
	}

	private void loadTitle() {
		title_feng = Sprite.make(GameSystem.tex_title_feng);
		title_feng.setPosition(73, 745);
		title_feng.setScale(0.6f);

		title_kuang = Sprite.make(GameSystem.tex_title_kuang);
		title_kuang.setPosition(133, 745);
		title_kuang.setScale(0.6f);

		title_tian = Sprite.make(GameSystem.tex_title_tian);
		title_tian.setPosition(193, 745);
		title_tian.setScale(0.6f);

		title_zi = Sprite.make(GameSystem.tex_title_zi);
		title_zi.setPosition(253, 745);
		title_zi.setScale(0.6f);

		addChild(title_feng);
		addChild(title_kuang);
		addChild(title_tian);
		addChild(title_zi);
	}

	public void onReturnClick() {
		GameUtil.switchSceneTopSlide(MainScene.make());
	}
}
